package cn.app.android.sichuanxiaoke.com.xiaokeapp.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;

import cn.app.android.sichuanxiaoke.com.xiaokeapp.R;

public class PaySuccessWebviewActivity extends AppCompatActivity {
    WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pay_success_webview);
        webView= (WebView) findViewById(R.id.webview);
        init();
        //initUI();
    }

    private void initUI() {
        Intent intent = getIntent();
        int status = intent.getIntExtra("status", 0);
        Log.i("status","-status----"+status);
        HttpParams params = new HttpParams();
        params.put("",status);
        OkGo.<String>post("").params(params).execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {

            }
        });
    }

    private void init() {
        Intent intent = getIntent();
        int status = intent.getIntExtra("status", 0);
        Log.i("status","-status----"+status);
        WebSettings settings = webView.getSettings();
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        settings.setSupportZoom(true);
        //打开与JS的交互权限
       settings.setJavaScriptEnabled(true);

        String format = String.format("http://112.74.173.191/index.php/Home/recharge/to_card_back?status=%s", status);
        webView.loadUrl(format);
       /* webView.post(new Runnable() {
            @Override
            public void run() {
                webView.loadUrl("javascript:PaySuccess("+istag+")");
            }
        });*/

        webView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });

        if(status == 1){
            AlertDialog.Builder builder=new AlertDialog.Builder(this);
            builder.setTitle("支付成功")
                    .setMessage("我们为尽快为您发货")
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                            finish();
                        }
                    });
            builder.create().show();
        }else {
            AlertDialog.Builder builder=new AlertDialog.Builder(this);
            builder.setTitle("支付失败")
                    .setMessage("抱歉，支付失败")
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                            finish();
                        }
                    });
            builder.create().show();
        }


    }
}
